import 'dart:convert';

class BaseModel {
  int status;
  String msg;
  dynamic data;

  BaseModel({
    this.status,
    this.msg,
    this.data,
  });

  factory BaseModel.fromJson(dynamic obj) {
    if (obj is String) {
      return BaseModel.fromMap(json.decode(obj));
    } else {
      return BaseModel.fromMap(obj);
    }
  }

  String toJson() => json.encode(toMap());

  factory BaseModel.fromMap(Map<String, dynamic> json) =>
      BaseModel(
          status: json["status"] == null
              ? json["code"] == null
                  ? null
                  : int.tryParse(json["code"].toString()) ?? 0
              : int.tryParse(json["status"].toString()) ?? 0,
          msg: json["msg"] == null ? null : json["msg"],
          data: json);

  Map<String, dynamic> toMap() => {
        "status": status == null ? null : status,
        "msg": msg == null ? null : msg,
      };
}
